package com.common.android.utils;

import android.os.Debug;
import android.util.Log;

/**
 * 堆内存相关类
 * <br>Created 2014-8-25 下午4:09:03
 * @version  
 * @author   huangyx		
 * @see 	 
 * Copyright(c) 2009-2014, TQ Digital Entertainment, All Rights Reserved
 *
 */
public class HeapObserverUtils {
    
    /**
     * 查看使用内存、未使用内存和总内存
     * <br>Created 2014-8-22 下午1:36:44
     * @author       huangyx
     */
    public static void check(){
//        long total=Debug.getNativeHeapSize();
        long used = Debug.getNativeHeapAllocatedSize();
        
        long max = Runtime.getRuntime().maxMemory();
        long free = Debug.getNativeHeapFreeSize();
//        long free=Runtime.getRuntime().freeMemory();
        long total = Runtime.getRuntime().totalMemory();
        Log.d("heapobserver", "max=" + max + ",total=" + total 
                + ",free=" + free + ",used=" + used);
    }
    
    /**
     * 获取内存使用率
     * <br>Created 2014-8-22 下午1:38:23
     * @return 内存使用率
     * @author       huangyx
     */
    public static float usedRate(){
        long max = Runtime.getRuntime().maxMemory();
        long used = Debug.getNativeHeapAllocatedSize();
        float rate = (float)used / (float)max;
        return rate;
    }
    
    /**
     * 查看内存使用状态
     * <br>Created 2014-8-22 下午1:40:07
     * @param tag 内存使用状态的日志打印的tsg
     * @author       huangyx
     */
    public static void check(String tag){
        Log.d("heapobserver", tag + ":" + getMemoryState());
    }
    
    /**
     * 获取内存最大值和使用大小
     * <br>Created 2014-8-22 下午1:41:48
     * @return 内存最大值和使用大小
     * @author       huangyx
     */
    public static String getMemoryState(){
    	long used = Debug.getNativeHeapAllocatedSize();
        long max = Runtime.getRuntime().maxMemory();
        return "max=" + max + ",used=" + used;
    }
}
